<template>
  <el-col class="info-wrapper" :span="23">
    <el-tabs type="border-card">
      <el-tab-pane>
        <span slot="label">密码修改</span>
        <el-form ref="passwordForm" class="password-form" :model="formParams" label-position="right" label-width="100px" :rules="rules">
          <el-form-item label="姓名 :" prop="name">
            <el-input disabled="true" v-model="formParams.name"></el-input>
          </el-form-item>
          <el-form-item label="用户名 :" prop="userName">
            <el-input disabled="true" v-model="formParams.userName"></el-input>
          </el-form-item>
          <el-form-item label="原密码 :" prop="oldPassword">
            <el-input v-model="formParams.oldPassword" show-password></el-input>
          </el-form-item>
          <el-form-item label="新密码 :" prop="newPassword">
            <el-input v-model="formParams.newPassword" show-password></el-input>
          </el-form-item>
          <el-form-item label="重新输入 :" prop="replayPassword">
            <el-input v-model="formParams.replayPassword" show-password></el-input>
          </el-form-item>
        </el-form>
        <el-button class="submit-btn" type="primary" @click="submitNewPassword">保 存</el-button>
      </el-tab-pane>
    </el-tabs>
  </el-col>
</template>

<script>
export default {
  name: 'changePassword',
  data() {
    const validatePass = (rule, value, callback) => {
      // 二次校验密码方法
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.formParams.newPassword) {
        callback(new Error("两次输入密码不一致"));
      } else {
        callback();
      }
    }

    return {
      formParams: {
        name: '王文英',
        userName: 'test9',
        oldPassword: '',
        newPassword: '',
        replayPassword: '',
      },
      rules: {
        oldPassword: { required: true, message: '请输入原密码', trigger: 'blur' },
        newPassword: { 
          required: true,
          message: '请输入新密码',
          trigger: 'blur'
        },
        replayPassword: { 
          required: true,
          validator: validatePass,
          trigger: 'blur'
        },
      }
    };
  },
  mounted() {
      
  },
  methods: {
    submitNewPassword() {
      this.$refs.passwordForm.validate();
      // todo: 接口提交，并验证旧密码正确性，并提示
      this.$message({
        message: '修改成功',
        type: 'success'
      });
    }
  },
};
</script>

<style lang="less" scoped>
.info-wrapper {
  font-family: Source Han Sans CN;
  font-weight: 400;
  height: 100%;

  .password-form {
    width: 380px;
  }

  .submit-btn {
    font-size: 14px;
    font-weight: 700;
    margin-top: 10px;
    margin-left: 400px
  }
}
</style>
